<!--
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2021-11-18 15:06:40
 * @LastEditors: voanit
 * @LastEditTime: 2021-12-08 08:58:46
-->
<template>
  <div class="emp-container">
    <page-tools>
      <!-- <div slot='right'> -->
      <!-- <template #right> -->
      <template v-slot:left>
        <div class="left">
          <i class="el-icon-info" />
          <span>总记录数：20条</span>
        </div>
      </template>
      <template v-slot:right>
        <el-button
          v-arrow="'download'"
          type="primary"
          size="small"
      
          @click="$router.push('/import')"
          >导入excel</el-button
        >
        <el-button type="info" size="small" @click="handleDownload"
          >导出excel</el-button
        >
        <el-button type="success" size="small" @click="addEmp"
          >添加员工</el-button
        >
      </template>
    </page-tools>

    <!-- 表格 -->
    <el-table
      :default-sort="{ prop: 'date', order: 'descending' }"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="150"> </el-table-column>
      <el-table-column sortable prop="username" label="姓名" width="120">
      </el-table-column>
      <el-table-column sortable prop="mobile" label="手机号" width="120">
      </el-table-column>
      <el-table-column sortable prop="workNumber" label="工号" width="120">
      </el-table-column>
      <el-table-column sortable label="聘用形式" width="130">
        <template slot-scope="scope">
          类型：{{ formatEmployment(scope.row.formOfEmployment) }}
        </template>
      </el-table-column>
      <el-table-column sortable prop="departmentName" label="部门" width="120">
      </el-table-column>
      <el-table-column sortable label="入职时间" width="120">
        <template slot-scope="scope">
          {{ scope.row.timeOfEntry | formatTimes }}
        </template>
      </el-table-column>
      <el-table-column sortable prop="enableState" label="是否在职" width="120">
      </el-table-column>
      <el-table-column sortable prop="inServiceStatus" label="状态" width="120">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template slot-scope="scope">
          <el-button
            @click="$router.push('/employees/detail/?id=' + scope.row.id)"
            type="text"
            size="small"
            >查看</el-button
          >

          <el-button
            @click="$router.push('/employees/detail/' + scope.row.id)"
            type="text"
            size="small"
            >查看/:id</el-button
          >
          <el-button type="text" size="small">转正</el-button>
          <el-button type="text" size="small">调岗</el-button>
          <el-button type="text" size="small">离职</el-button>
          <el-button type="text" size="small" @click="addRoles(scope.row.id)"
            >角色</el-button
          >
          <el-button type="text" size="small" @click="del(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="paramsObj.page"
      :page-size="paramsObj.size"
      :page-sizes="[1, 3, 5, 7, 10]"
      layout="slot, prev, pager, next"
      :total="total"
      >共{{ total }}条
    </el-pagination>

    <!-- 对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="45%">
      <!-- 表单布局==抽里程一个公共组件 -->
      <add-emp-dia @success="success"></add-emp-dia>
    </el-dialog>

    <!-- 分配角色 -->
    <el-dialog
      :visible.sync="showDialogRole"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <!-- <assign-role ref='assignRole' v-if="showDialogRole" :empId="empId" @close="close" /> -->
      <assign-role
        v-if="showDialogRole"
        ref="assignRole"
        :empId="empId"
        @close="close"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeListApi, delEmployeeApi } from '@/api/employees'
import EmployeeEnum from '@/constant/employees'
import addEmpDia from './addEmpDia.vue'
import assignRole from './assignRole.vue'

export default {
  name: '',
  components: {
    addEmpDia,
    assignRole
  },

  data() {
    return {
      tableData: [],
      paramsObj: {
        page: 1,
        size: 5
      },
      total: 0,
      dialogVisible: false,
      showDialogRole: false,
      empId: ''
    }
  },
  created() {
    this.getEmp()
  },
  methods: {
    /* 分页 */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.paramsObj.size = val
      this.getEmp()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.paramsObj.page = val
      this.getEmp()
    },
    async getEmp() {
      const res = await getEmployeeListApi(this.paramsObj)
      console.log(res, 5)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    handleClick(row) {
      console.log(row)
    },
    formatEmployment(num) {
      // 从hireType中 通过id找到对应的元素的value
      // console.log(EmployeeEnum, 88)
      const { hireType } = EmployeeEnum
      const item = hireType.find((it) => it.id === num)
      if (item) {
        return item.value
      } else {
        return '不能确定'
      }
      // return  ? '正式' : '非正式'
    },
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delEmployeeApi(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          //  刷新页面

          this.getEmp()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addEmp() {
      /*
      1、显示对话框
      2、表单布局
      3、校验
      4、获取部门数据===参考组织架构模块
      5、点击确认按钮
        5.1 写api接口函数
        5.2 校验
        5.3 发送请求
        5.4 关闭对话框+刷新页面+清空白单数据

       */
      this.dialogVisible = true
    },

    success() {
      // 关闭对话框+刷新页面
      this.dialogVisible = false
      this.getEmp()
    },

    /* 导出 */
    async handleDownload() {
      /* 虎丘所有的员工总数 */
      const res = await getEmployeeListApi({ page: 1, size: this.total })
      console.log(res, 5)
      this.tableData = res.data.rows
      import('@/vendor/Export2Excel').then((excel) => {
        /* 设置表头的 */
        // const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date', 'name']
        const list = this.tableData
        console.log(list, 5566)
        const { header, data } = this.formatJson(list)
        excel.export_json_to_excel({
          multiHeader: [['id', 'password', '', '', '', '', 'qqq', '', '', '']], // 设置多表头
          header: header,
          merges: ['H1:I1'], //  合并
          data: data,
          filename: 'web68', //  设置excel文件名
          autoWidth: true, //  设置表格宽度自适应
          bookType: 'xlsx' // 生成文件后缀名
        })
      })
    },
    formatJson(list) {
      //  最终返回的是一个二维数组  处理表头
      const map = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      var header = []
      var data = []
      header = Object.values(map) // ['编号', '密码', '手机号', '姓名', '入职日期', '聘用形式', '转正日期', '工号', '部门', '头像地址']
      data = list.map((item) => {
        var obj = {}
        var enKey = Object.keys(map) //  英文键 ['id', 'password', 'mobile', 'username', 'timeOfEntry', 'formOfEmployment', 'correctionTime', 'workNumber', 'departmentName', 'staffPhoto']
        // console.log(enKey, 7)
        enKey.forEach((key) => {
          obj[key] = item[key]
        })
        /*
        obj===
        correctionTime: "2018-11-30T00:00:00.000+0000"
        departmentName: "总裁办"
        formOfEmployment: 1
        id: "1063705989926227968"
        mobile: "13800000002"
        password: "c8b7722b1139bb9b346409e503302e82"
        staffPhoto: "http://rzzt-1308693926.cos.ap-chongqing.myqcloud.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211209101221.jpg"
        timeOfEntry: "2018-11-02T08:00:00.000+0000"
        username: "管理员"
        workNumber: "9002"
         */
        return Object.values(obj)
      })
      return { header, data }
    },

    /* 点击角色 */
    addRoles(id) {
      /*
      1、显示对话框
      2、获取所有的角色
      3、获取用户当前所拥有的角色==并选中
      4、配置角色
        4.1 封账api接口函数
        4.2 发请求
        4.3 关闭对话框
       */
      this.showDialogRole = true
      this.empId = id

      this.$nextTick(() => {
        this.$refs.assignRole.getRoles() //  重新获取角色
      })
    },
    /* 子组件点击取消或者确认  关闭对话框 */
    close() {
      this.showDialogRole = false
    }
  }
}
</script>
<style lang='scss'  scoped>
.left {
  padding: 10px;
  border: 1px solid #c2e8ff;
  border-radius: 5px;
  background-color: #c2e8ff;
  .el-icon-info {
    color: #4199ff;
    margin-right: 5px;
  }
}

.el-pagination {
  text-align: right;
}
.emp-container {
  ::v-deep .el-dialog__header {
    height: 50px;
  }

  /*  穿透
  scss  ::v-deep  提升权重
  less  /deep/
      /deep/ .el-dialog__header {
        height: 50px;
      }
  */
}
</style>
